package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Resource
    private ShoppingCartMapper shoppingCartMapper;

    @Resource
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        Long dishId = shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();
        String dishFlavor = shoppingCartDTO.getDishFlavor();


        List<ShoppingCart> list = shoppingCartMapper.findRecord(shoppingCartDTO);

        // 之前有记录了，直接数量加一即可
        if(list != null && list.size() > 0 ) {
            ShoppingCart shoppingCart = list.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
//            shoppingCart.setAmount();
            shoppingCartMapper.update(shoppingCart);
            return;
        }

        ShoppingCart shoppingCart = new ShoppingCart();

        // 如果之前没有的话，就是一条新的记录，然后我们要看一下是套餐还是菜品
        if(dishId != null) {
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setDishId(dishId);
            shoppingCart.setSetmealId(setmealId);
            shoppingCart.setDishFlavor(dishFlavor);

            // 下面的就要进行表查询操作
            Dish dish = dishMapper.selectById(dishId);
            shoppingCart.setName(dish.getName());
            shoppingCart.setAmount(dish.getPrice());
            shoppingCart.setImage(dish.getImage());
        }

        if(setmealId != null) {
            Setmeal setmeal = setmealMapper.getById(setmealId);
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setDishId(dishId);
            shoppingCart.setSetmealId(setmealId);
            shoppingCart.setDishFlavor(dishFlavor);
            shoppingCart.setAmount(setmeal.getPrice());
            shoppingCart.setImage(setmeal.getImage());

        }
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());

        shoppingCartMapper.save(shoppingCart);

    }

    @Override
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> list = shoppingCartMapper.list(userId);
        return list;
    }

    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(userId);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        List<ShoppingCart> record = shoppingCartMapper.findRecord(shoppingCartDTO);
        if(record == null || record.size() == 0) {
            throw new RuntimeException("购物车没有该记录");
        }
        ShoppingCart shoppingCart = record.get(0);
        if(shoppingCart.getNumber() == 1) {
            shoppingCartMapper.deleteById(shoppingCart.getId());
            return;
        }
        shoppingCart.setNumber(shoppingCart.getNumber() - 1);
        shoppingCartMapper.update(shoppingCart);
    }
}
